package com.wsj.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wsj.dao.IDirectSeedNameDao;
import com.wsj.util.page.PageResultSet;
import com.wsj.util.page.PerfectPageResultSet;
import com.wsj.util.tool.Tool;
import com.wsj.web.vo.homeSite.DirectSeedName;

public class DirectSeedNameDaoImpl extends HibernateDaoSupport implements
		IDirectSeedNameDao {

	public void add(DirectSeedName directSeedName) throws DataAccessException {
		this.getHibernateTemplate().save(directSeedName);
	}

	public void delete(DirectSeedName directSeedName) throws DataAccessException {
		this.getHibernateTemplate().delete(directSeedName);
	}

	@SuppressWarnings("unchecked")
	public List<DirectSeedName> loadAll() throws DataAccessException {
		String sql = " from DirectSeedName where 1 = 1";
		return this.getHibernateTemplate().find(sql);
	}

	public DirectSeedName loadById(Integer id) throws DataAccessException {
		Object obj = null;
		DirectSeedName directSeedName = new DirectSeedName();
		obj =  this.getHibernateTemplate().get(DirectSeedName.class, id);
		if(obj!=null){
			directSeedName = (DirectSeedName)obj;
		}
		return directSeedName;
	}

	public DirectSeedName loadByName(String name) throws DataAccessException {
		String sql = " from DirectSeedName as d where d.qiname = ? ";
		DirectSeedName d  = null;
		List list = this.getHibernateTemplate().find(sql,name);
		if(list != null && list.size()>0){
			d = (DirectSeedName)list.get(0);
		}
		return d;
	}

	public List loadDirectSeedNameByInputtime(String year)
			throws DataAccessException {
		List list=new ArrayList();
		int yea=Integer.valueOf(year)+1;
		if(year!=null&&!year.equals("-1")&&!year.equals("")){
			String sql="from DirectSeedName as d where d.inputtime >= ? and d.inputtime < ?";
			list=this.getHibernateTemplate().find(sql,new String[]{year,String.valueOf(yea)});
		}
		return list;
	}

	public PageResultSet loadPageList(String keyWord, String name,
			Short iscommend, Short del, String beginDate, String endDate,
			Integer page, Integer pageSize) throws DataAccessException {
		PageResultSet pageResultSet = new PageResultSet();
	    PerfectPageResultSet pageResult = new PerfectPageResultSet();
	    // 获取Session
	    Session session = SessionFactoryUtils.getSession(getSessionFactory(),true);

	    // 初始化SQL语句
	    StringBuffer stringBuffer = new StringBuffer(
	            " from DirectSeedName as d  where 1=1 ");
	    
	    // 设置每页显示得记录数和开始页
	    pageResult.setPage(page, pageSize);

	    // 参数
	    List<Comparable> list = new ArrayList<Comparable>();
	    
	    // 直播关键词
	    if(keyWord!=null && !keyWord.equals(""))
	    {
	    	keyWord = "%"+keyWord+"%";
	        stringBuffer.append(" and d.gjc like ?");
	        list.add(keyWord);
	    }
	    
	    // 直播名
	    if(name!=null && !name.equals(""))
	    {
	    	name = "%"+name+"%";
	        stringBuffer.append(" and d.qiname like ?");
	        list.add(name);
	    }
	    
	    // 是否推荐 0 否 1 是
	    if(iscommend!=null&&!iscommend.equals((short)-1))
	    {
	    	stringBuffer.append(" and d.iscommend = ?");
	        list.add(iscommend);
	    }
	    
	    // 是否删除 0 否 1 是
	    if(del!=null&&!del.equals((short)-1))
	    {
	    	stringBuffer.append(" and d.del = ?");
	        list.add(del);
	    }
	    
	    // 时间段
	    if(null!=beginDate && !"".equals(beginDate) && null!=endDate && !"".equals(endDate)){
	    	stringBuffer.append(" and d.inputtime >= '"+beginDate.trim()+" 00:00:00' and d.inputtime <= '"+endDate.trim()+" 23:59:59'");
	    }
	    
	    stringBuffer.append(" order by id desc ");

	    // 把集合转换成数组
	    String[] strParams = Tool.toArray(list);
	    
	    pageResultSet = pageResult.pageResultSet(stringBuffer.toString(),session, strParams);
	    
	    return pageResultSet;
	}

	public void update(DirectSeedName directSeedName) throws DataAccessException {
		this.getHibernateTemplate().saveOrUpdate(directSeedName);
	}

	public DirectSeedName getZuiXinTopDirectSeedName(int top) throws DataAccessException {
		// TODO Auto-generated method stub
		DirectSeedName directSeedName=null;
		String sql=" from DirectSeedName as d where 1=1 and d.shipin !=null and d.shipin!='' and d.del=0  order by d.inputtime desc";
		Query q=this.getSession().createQuery(sql);
		q.setFirstResult(0);
		q.setMaxResults(top);
		List list=q.list();
		if(list!=null&&!list.isEmpty()){
			directSeedName=(DirectSeedName) list.get(0);
		}
		
		
		return directSeedName;
		
	}

	public List getTopDirectSeedName(int top, Integer sameId) throws DataAccessException {
		// TODO Auto-generated method stub
		String sql=" from DirectSeedName as d where 1=1 and d.id != "+sameId+" and d.iscommend=1 and d.del=0  order by d.inputtime desc";
		Query q=this.getSession().createQuery(sql);
		q.setFirstResult(0);
		q.setMaxResults(top);
		return q.list();
	}

	public DirectSeedName getzuixinDSeedName() throws DataAccessException {
		// TODO Auto-generated method stub
		
		DirectSeedName directSeedName=null;
		String sql=" from DirectSeedName as d where 1=1 and d.del=0 order by d.inputtime desc";
		List list=this.getHibernateTemplate().find(sql);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			directSeedName=(DirectSeedName) list.get(0);
		}
		return directSeedName;
	}

	public List getTopByZxinDirectSeedName(Integer zxinDSeedNameid, int top) throws DataAccessException {
		// TODO Auto-generated method stub
		String sql=" from DirectSeed as d where 1=1 and d.directSeedClass.directSeedName.id = ? and d.isimg=1 and d.islock=0  and d.shanchu=0  order by d.datetime desc";
		Query q=this.getSession().createQuery(sql);
		q.setParameter(0, zxinDSeedNameid);
		q.setFirstResult(0);
		q.setMaxResults(top); 
		return q.list();
	}

}













